<?php

/**
 *
 * Wechatpay.php
 *
 */
class Weixinpay extends Think
{
    public $config = array();

    public function __construct($config = array())
    {
        $this->config = $config;

        $this->config['gateway_url'] = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        $this->config['gateway_method'] = 'POST';
        $this->config['notify_url'] = return_url('Wechatpay', 1);
        $this->config['return_url'] = return_url('Wechatpay');
    }

    public function setup()
    {

        $modules['pay_name'] = '微信支付';
        $modules['pay_code'] = 'Wechatpay';
        $modules['pay_desc'] = '微信支付介绍';
        $modules['is_cod'] = '0';
        $modules['is_online'] = '1';
        $modules['author'] = '傲鸿科技';
        $modules['website'] = 'http://pay.weixin.qq.com';
        $modules['version'] = '1.0.0';
        $modules['config'] = array(
            array(
                'name'  => 'APPID',
                'type'  => 'text',
                'value' => ''
            ),
            array(
                'name'  => 'MCHID',
                'type'  => 'text',
                'value' => ''
            ),
            array(
                'name'  => 'KEY',
                'type'  => 'text',
                'value' => ''
            ),
            array(
                'name'  => 'APPSECRET',
                'type'  => 'text',
                'value' => ''

            )
        );

        return $modules;
    }

    public function get_code()
    {

        $parameter = array(
            'service'           => $this->config['service'],
            'partner'           => trim($this->config['alipay_partner']),
            '_input_charset'    => 'utf-8',
            'notify_url'        => trim($this->config['notify_url']),
            'return_url'        => trim($this->config['return_url']),
            /* 商品信息 */
            'subject'           => $this->config['order_sn'],
            'out_trade_no'      => $this->config['order_sn'],
            'price'             => $this->config['order_amount'],
            'body'              => $this->config['body'],
            'quantity'          => 1,
            'payment_type'      => 1,
            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',
            //'agent'             => $this->config['agent'],

            /* 买卖双方信息 */
            'seller_email'      => trim($this->config['alipay_account'])
        );
        ksort($parameter);
        reset($parameter);
        $param = '';
        $sign = '';

        foreach ($parameter AS $key => $val) {
            $param .= "$key=" . urlencode($val) . "&";
            $sign .= "$key=$val&";
        }

        $param = substr($param, 0, -1);
        $sign = substr($sign, 0, -1) . $this->config['KEY'];
        //$sign  = substr($sign, 0, -1). ALIPAY_AUTH;

        //      $button = '<span><input type="button" class="button inline px24 fon-white bg-orange txt_c pt5 pb5 hover-yellow bdy-grey pl20 pr20" onclick="window.open(\''.$this->config['gateway_url'].$param. '&sign='.MD5($sign).'&sign_type=MD5\')" value="'.L('PAY_NOW').'" /></span>';
        $button = $this->config['gateway_url'] . $param . '&sign=' . MD5($sign) . '&sign_type=MD5';

        return $button;
    }

    public function respond()
    {
        if (!empty($_POST)) {
            foreach ($_POST as $key => $data) {
                $_GET[$key] = $data;
            }
        }

        $seller_email = rawurldecode($_GET['seller_email']);
        //$order_sn = str_replace($_GET['subject'], '', $_GET['out_trade_no']);
        $order_sn = trim($_GET['out_trade_no']);

        /* 检查数字签名是否正确 */
        ksort($_GET);
        reset($_GET);

        $sign = '';
        foreach ($_GET AS $key => $val) {
            if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key != 'g' && $key != 'm' && $key != 'a') {
                $sign .= "$key=$val&";
            }
        }

        $sign = substr($sign, 0, -1) . $this->config['alipay_key'];

        //$sign = substr($sign, 0, -1) . ALIPAY_AUTH;
        if (md5($sign) != $_GET['sign']) {
            return false;
        }

        if ($_GET['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || $_GET['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS' || $_GET['trade_status'] == 'WAIT_BUYER_PAY') {
            /* 改变订单状态 进行中*/
            order_pay_status($order_sn, '1');
            return true;
        } elseif ($_GET['trade_status'] == 'TRADE_FINISHED') {
            /* 改变订单状态 */
            order_pay_status($order_sn, '2');
            return true;
        } elseif ($_GET['trade_status'] == 'TRADE_SUCCESS') {
            /* 改变订单状态 即时交易成功*/
            order_pay_status($order_sn, '2');
            return true;
        } else {
            return false;
        }
    }

}